package com.linkage.bookshop.service.impl;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.linkage.bookshop.model.Role;
import com.linkage.bookshop.model.User;
import com.linkage.bookshop.repository.UserRepository;
import com.linkage.bookshop.service.inter.UserService;


@Service("userService")
public class UserServiceImpl implements UserService {
	
	@Resource
	private UserRepository userRepository;

	@Override
	public boolean checkUsername(String username) {
		if(userRepository.selectUsername(username) == null){
			return true;
		}
		return false;
	}
	
	@Override
	public boolean checkPassword(String username, String oldPassword) {
		String password = userRepository.selectPassword(username);
		if(oldPassword.equals(password)){
			return true;
		}
		return false;
	}
	
	@Override
	@Transactional
	public boolean addUser(String username, String password) {
		if(username == null || !username.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")){
			return false;
		}
		if(password.length()<6 || password.length()>15){
			return false;
		}
		boolean result = checkUsername(username);
		if(!result){
			return false;
		}
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setNickname(username);
		user.setWealth(1000.0);
		user.setNonlocked(true);
		userRepository.insertUser(user);
		userRepository.insertUserAndRoleRelation(user.getId(),5);
		return true;
	}
	
	@Override
	public String loadNickname(String username) {
		if(username == null || username.equals("")){
			return null;
		}
		return userRepository.selectNickname(username);
		
	}
	
	@Override
	public Double loadWealth(String username) {
		if(username == null || username.equals("")){
			return null;
		}
		return userRepository.selectWealth(username);
	}

	@Override
	public boolean modifyPassword(String username,String password) {
		if(username == null || password == null){
			return false;
		}
		if(password.length()<6 || password.length()>15){
			return false;
		}
		
		userRepository.updatePassword(username, password);
		return true;
	}

	@Override
	public boolean modifyNickname(String username, String nickname) {
		if(username == null || username.equals("") || nickname == null || nickname.equals("")){
			return false;
		}
		
		if(nickname.length()>15){
			return false;
		}
		
		if(!nickname.matches("[\\w_\\u0391-\\uFFE5]+")){
			return false;
		}
		userRepository.updateNickname(username,nickname);
		return true;
	}


	
	@Override
	public List<User> listMembers(int numPerPage, int pageNum) {
		int begin = (pageNum-1)*numPerPage;
		if( begin<0 || pageNum<0){
			return null;
		}
		return userRepository.selectMembers(begin, numPerPage);
	}

	@Override
	public List<User> listAdmins(int numPerPage, int pageNum) {
		int begin = (pageNum-1)*numPerPage;
		if( begin<0 || pageNum<0){
			return null;
		}
		return userRepository.selectAdmins(begin, numPerPage);
	}

	@Override
	public boolean lockMember(int id) {
		userRepository.updateNonlocked(id,false);
		return true;
	}

	@Override
	public boolean lockAdmin(int id) {
		userRepository.updateNonlocked(id,false);
		return true;
	}

	@Override
	public boolean unlockMember(int id) {
		userRepository.updateNonlocked(id,true);
		return true;
	}

	@Override
	public boolean unlockAdmin(int id) {
		userRepository.updateNonlocked(id,true);
		return true;
	}

	@Override
	public boolean addAdmin(User admin) {
		if(admin == null){
			return false;
		}
		if(admin.getUsername() == null||!admin.getUsername().matches("[\\w_]+")){
			return false;
		}
		boolean result = checkUsername(admin.getUsername());
		if(!result){
			return false;
		}
		if(admin.getPassword() == null || admin.getPassword().length()<6 ||admin.getPassword().length()>15 ){
			return false;
		}
		if(admin.getNickname() == null || !admin.getNickname().matches("[\\u0391-\\uFFE5]{1,5}")){
			return false;
		}
		Iterator<Role> iterator = admin.getRoles().iterator();
		int[] roleIds = new int[3];
		int j = 0;
		while(iterator.hasNext()){
			
			String roleName = iterator.next().getName();
			if(!roleName.equals("ADM_MEMBER") && !roleName.equals("ADM_GOODS") && !roleName.equals("ADM_ORDER")){
				return false;
			}
			roleIds[j] = userRepository.selectRoleId(roleName); 
			j++;
		}
		admin.setNonlocked(true);
		admin.setWealth(0.0);
		userRepository.insertUser(admin);
		for(int i=0;i<3;i++){
			if(roleIds[i] != 0){
				userRepository.insertUserAndRoleRelation(admin.getId(),roleIds[i]);
			}
		}
		return true;
	}

	@Override
	public User loadMember(String username) {
		return userRepository.selectMember(username); 
	}

	@Override
	public User loadAdmin(String username) {
		return userRepository.selectAdmin(username);
	}	
}
